/**
 * Created by Administrator on 2025/5/29.
 * */
#include "AIE2EInferController.h"
#include <iostream>
#include <thread>

BaseObject *createAIE2EInferController() {
    return new AIE2EInferController();
}

void destroyAIE2EInferController(BaseObject *p) {
    SAFE_DELETE(p);
}

AIE2EInferController::AIE2EInferController(){
    prevStep = -1;
    // 订阅数据源主题Topic
    sub = zmq::socket_t(context, zmq::socket_type::sub);
    sub.connect(topicAddr.c_str());
    sub.set(zmq::sockopt::subscribe, topicName);
}

AIE2EInferController::~AIE2EInferController() {
}

/* 如果使用了DDS_Reader或者DDS_Writer，则对应的input或者output前64个字节为DataFormat head首部 */
void AIE2EInferController::onCompute(buffer_table_t *input, buffer_table_t *output) {
    std::this_thread::sleep_for(std::chrono::milliseconds(1000));
    int step;
    zmq::message_t msgTopicName;
    zmq::message_t msgData;
    if (!sub.recv(msgTopicName, zmq::recv_flags::none)) {
        std::cerr << "Subscribe: Failed to receive topic name" << std::endl;
    }
    std::string topicName_(static_cast<char*>(msgTopicName.data()), msgTopicName.size());
    if (!sub.recv(msgData, zmq::recv_flags::none)) {
        std::cerr << "Subscribe: Failed to receive data" << std::endl;
    }
    memcpy(&step, msgData.data(), sizeof(float));
    if (prevStep == step) {
        return ;
    }
    prevStep = step;
    if (input->list[1]->valid) {
        int *pAIE2EState = reinterpret_cast<int*>(input->list[1]->data);
        if (*pAIE2EState > 0) {
            auto aiE2EStateHolder = GlobalParameters::shareInstance()->findParameter("aiE2EState");
            int *aiE2EState = static_cast<int*>(GlobalParameters::shareInstance()->getValue(aiE2EStateHolder, dynamic_cast<TaskModule*>(this)));
            *aiE2EState = *pAIE2EState;
            std::cout << "AIE2EInfer::AIE2EInferController 接收到AIE2E模块模型升级通知：state=" << *pAIE2EState << "; !!!!!!!!!!" << std::endl;
        }
    }
}
